package com.twodudesdev.amerikick.learnkarate;

import android.app.TabActivity;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.os.Vibrator;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TabHost.TabSpec;
import android.widget.TextView;
import android.widget.Toast;

public class MainTabActivity extends TabActivity  {
	private TabHost mTabHost;
	
	static final String LOCATION_ACTION = "com.twodudesdev.amerikick.learnkarate.LOCATION";
	
	static final String STRING_LOCATION = "com.twodudesdev.amerikick.learnkarate.string_location";
	
	
	void showToast(CharSequence msg) {
        Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
    }
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
    	super.onCreate(savedInstanceState);
    	setContentView(R.layout.main);
    	
    	mTabHost = (TabHost) findViewById(android.R.id.tabhost);
        mTabHost.setOnTabChangedListener(new OnTabChangeListener(){
        	@Override
        	public void onTabChanged(String tabId) {
        		// Get instance of Vibrator from current Context
            	Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
            	 
            	// Vibrate for 300 milliseconds
            	v.vibrate(100);
        	}});
    	Resources res = getResources();
    	
    	//mTabHost.getTabWidget().setDividerDrawable(R.drawable.tab_divider);
		
    	setupTab(new Intent().setClass(this,HomeActivity.class), "Home", res.getDrawable(R.drawable.tab_gate));
    	setupTab(new Intent().setClass(this,XMAActivity.class), "XMA", res.getDrawable(R.drawable.tab_kick));
    	setupTab(new Intent().setClass(this,VideoActivity.class), "Training", res.getDrawable(R.drawable.tab_defense));
    	
    	Intent intent = new Intent(LOCATION_ACTION);;
	    intent.putExtra(STRING_LOCATION, "AllStates");
		 
    	setupTab(intent, "Locations", res.getDrawable(R.drawable.tab_location));
	}
	
    private void setupTab(/*final View view,*/ final Intent intent, final String tag, Drawable logo) {
    	View tabview = createTabView(mTabHost.getContext(), tag, logo);
            TabSpec setContent = mTabHost.newTabSpec(tag).setIndicator(tabview).setContent(intent);/*new TabContentFactory() {
    		@Override
			public View createTabContent(String tag) {return view;}
    	});*/
    	mTabHost.addTab(setContent);
    }

    private static View createTabView(final Context context, final String text, final Drawable image) {
    	View view = LayoutInflater.from(context).inflate(R.layout.tabs_bg, null);
    	TextView tv = (TextView) view.findViewById(R.id.tabsText);
    	tv.setText(text);
    	ImageView iv = (ImageView) view.findViewById(R.id.tabsImage);
    	iv.setImageDrawable(image);
    	iv.setAdjustViewBounds(true);
    	iv.setMaxHeight(60);
    	iv.setMaxWidth(60);
    	return view;
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    	MenuInflater inflater = getMenuInflater();
	    inflater.inflate(R.menu.menu, menu);
	    return true;
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
    	Intent i;
		// Handle item selection
	    switch (item.getItemId()) {
	    case R.id.contact_us:
	    	i = (new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.amerikick.com/index.php?option=com_aicontactsafe&view=message&layout=message&pf=1&Itemid=55")));
	    	startActivity(i);
	        return true;
	    default:
	        return super.onOptionsItemSelected(item);
	    }
    }
}
